<template>
    <div style="margin: 20px;">
        <div>
            <Row style="margin-bottom: 25px;">
                <Col span="5" style="margin-right: 20px;">
                    <Select v-model="classNum" filterable clearable placeholder="请选择班级名称...">
                        <Option v-for="item in classList" :value="item.value" :key="item.value">{{ item.label }}
                        </Option>
                    </Select>
                </Col>
                <Col span="2">
                    <Button type="primary" shape="circle" icon="ios-search" @click="search()">搜索</Button>
                </Col>
            </Row>
        </div>
        <!--列表model-->
        <div>
            <ul style="list-style: none">
                <li>
                    <Button type="primary" icon="plus-round" @click="insertClass()">新建</Button>
                    <Button type="error" icon="trash-a" @click="del()">删除</Button>
                </li>
                <li>
                    <div style="padding: 10px 0;position: relative;">
                        <Spin fix v-if="loadingTable">
                            <Icon type="load-c" size=18 class="demo-spin-icon-load"></Icon>
                            <div>Loading</div>
                        </Spin>
                        <Table border :columns="classColumn" :data="classData" @on-selection-change="s=>{change(s)}"></Table>
                    </div>
                </li>
                <li>
                    <div style="text-align: right;">
                        <Page :total="total" :page-size="pageInfo.pageSize" show-elevator show-total
                              @on-change="e=>{pageSearch(e)}"></Page>
                    </div>
                </li>
            </ul>
        </div>
        <!--添加modal-->
        <Modal :mask-closable="false" :visible.sync="newModal" :loading="loading" v-model="newModal" width="600"
               title="新建" @on-ok="newOk('classNew')" @on-cancel="cancel(0)">
            <Form ref="classNew" :model="classNew" :rules="ruleNew" :label-width="80">
                <Row>
                    <Form-item label="班级编号:" prop="classNum">
                        <Input v-model="classNew.classNum" number style="width: 90%"/>
                    </Form-item>
                </Row>
                <Row>
                    <Form-item label="班级名称:" prop="className">
                        <Input v-model="classNew.className" style="width: 90%"/>
                    </Form-item>
                </Row>
            </Form>
        </Modal>
        <!--修改modal-->
        <Modal :mask-closable="false" :visible.sync="modifyModal" :loading="loading" v-model="modifyModal" width="600"
               title="编辑" @on-ok="modifyOk('classModify')" @on-cancel="cancel(1)">
            <Form ref="classModify" :model="classModify" :rules="ruleModify" :label-width="80">
                <Row>
                    <Form-item label="班级编号:" prop="classNum">
                        <Input v-model="classModify.classNum" number style="width: 90%"/>
                    </Form-item>
                </Row>
                <Row>
                    <Form-item label="课程名称:" prop="className">
                        <Input v-model="classModify.className" style="width: 90%"/>
                    </Form-item>
                </Row>
            </Form>
        </Modal>
    </div>
</template>
<script>
    function isNotBlank(e) {
        if (e != null && e != '' && e != undefined) {
            return true;
        }
        return false;
    }
    export default {
        data() {
            const validateClassNum = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('班级编号不能为空'));
                }
                // 模拟异步验证效果
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字类型'));
                    } else {
                        if (value < 0) {
                            callback(new Error('编号必须大于0'));
                        } else if(value > 10000) {
                            callback(new Error('编号必须小于10000'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            return {
                loadingTable:true,
                classNum: 0,
                count: null,
                groupId: null,
                newModal: false,
                modifyModal: false,
                total: 0,
                loading: true,
                pageInfo: {
                    page: 0,
                    pageSize: 10,
                    classNum: 0
                },
                classList: [],
                /*class实体*/
                class: {
                    classNum: null,
                    className: null
                },
                /*用于添加的class实体*/
                classNew: {
                    classNum: null,
                    className: null
                },
                /*用于修改的class实体*/
                classModify: {
                    classNum: null,
                    className: null
                },
                /**********************变量定义end**********************/
                /**********************页面校验start**********************/
                /*新建验证*/
                ruleNew: {
                    classNum: [
                        {validator: validateClassNum, trigger: 'blur'}
                    ],
                    className: [
                        {type: 'string', required: true, max: 100, message: '输入班级名称', trigger: 'blur'}
                    ]
                },
                /*修改验证*/
                ruleModify: {
                    classNum: [
                        {validator: validateClassNum, trigger: 'blur'}
                    ],
                    className: [
                        {type: 'string', required: true, message: '输入班级名称', trigger: 'blur'}
                    ]
                },
                /**********************页面校验end**********************/
                /**********************列表显示start**********************/
                /*生产类型表显示字段*/
                classColumn: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '班级编号',
                        key: 'classNum',
                        align: 'center'
                    },
                    {
                        title: '班级名称',
                        key: 'className',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        width: '100%',
                                        overflow: 'hidden',
                                        textOverflow: 'ellipsis',
                                        whiteSpace: 'nowrap'
                                    },
                                    attrs: {
                                        title: params.row.className
                                    },
                                }, params.row.className)
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        width:160,
                        render: (h, params) => {
                            let btnArr = [];
                            btnArr.push(
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        margin: '3px',
                                        color: '#2d8cf0',
                                        backgroundColor: '#ffffff',
                                        border: '1px solid #2d8cf0'
                                    },
                                    on: {
                                        click: () => {
                                        this.relationEdit(params.row)
                                        }
                                     }
                                }, '编辑'),
                            );
                            btnArr.push(
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    style: {
                                        margin: '3px',
                                        color: '#ed3f14',
                                        backgroundColor: '#ffffff',
                                        border: '1px solid #ed3f14'
                                    },
                                    on: {
                                        click: () => {
                                            this.setSingleGroupId(params.row.classNum);
                                            this.del();
                                        }
                                    }
                                 }, '删除')
                            );
                        return h('div', btnArr);
                        }
                    }
                ],
                /*生产类型表数据*/
                classData: []
                /**********************列表显示start**********************/
            }
        },
        mounted() {
            /*页面初始化调用方法*/
            this.getTable(this.pageInfo);
            this.getAllClass();
        },
        methods: {
            /*pageInfo实体初始化*/
            initPageInfo() {
                this.pageInfo.page = 0;
                this.pageInfo.pageSize = 10;
                this.pageInfo.classNum = 0;
            },
            /*class实体初始化*/
            initClass() {
                this.class.classNum = null;
                this.class.className = null;
            },
            /*classNew实体初始化*/
            initClassNew(){
                this.classNew.classNum = null;
                this.classNew.className = null;
            },
            /*classModify实体初始化*/
            initClassModify(){
                this.classModify.classNum = null;
                this.classModify.className = null;
            },
            /*class设置*/
            classSet(e) {
                this.class.classNum = e.classNum;
                this.class.className = e.className;
            },
            /*classNew设置*/
            classNewSet(e){
                this.classNew.classNum = e.classNum;
                this.classNew.className = e.className;
            },
            /*classModify设置*/
            classModifySet(e){
                this.classModify.classNum = e.classNum;
                this.classModify.className = e.className;
            },
            /*获取列表数据*/
            getTable(e) {
                this.loadingTable = true;
                this.axios({
                    method: 'get',
                    url: '/class',
                    params: {
                        page: e.page,
                        pageSize: e.pageSize,
                        classNum: isNotBlank(e.classNum) ? e.classNum : 0
                    }
                }).then(function (response) {
                    this.classData = response.data.data;
                    this.total = response.data.totalCount;
                    this.loadingTable = false;
                    this.groupId = null;
                    this.initPageInfo();
                }.bind(this)).catch(function (error) {
                    //alert(error);
                });
            },
            /*搜索按钮点击事件*/
            search() {
                this.pageInfo.classNum = this.classNum;
                this.getTable(this.pageInfo);
            },
            /*分页点击事件*/
            pageSearch(e) {
                this.pageInfo.page = e - 1;
                this.getTable(this.pageInfo);
            },
            /*新建点击触发事件*/
            insertClass() {
                this.$refs['classNew'].resetFields();
                this.newModal = true;
                this.classData.sort();
                this.count = 0;
                this.groupId = null;
            },
            /*新建modal的newOk点击事件*/
            newOk(classNew) {
                this.$refs[classNew].validate((valid) => {
                    if (valid) {
                        this.initClass();
                        this.classSet(this.classNew);
                        this.axios({
                            method: 'post',
                            url: '/class',
                            data: this.class
                        }).then(function (response) {
                            if (response.data && response.data.msg == 'success') {
                                this.$Message.info('新建成功');
                            }
                            this.initClass();
                            this.getTable(this.pageInfo);
                        }.bind(this)).catch(function (error) {
                            //alert(error);
                        });
                        this.newModal = false;
                    } else {
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 1000);
                    }
                })
            },
            /*点击修改时*/
            relationEdit(e){
                this.$refs['classModify'].resetFields();
                this.classModifySet(e);
                this.modifyModal = true;
            },
            /*修改modal的modifyOk点击事件*/
            modifyOk(classModify) {
                this.$refs[classModify].validate((valid) => {
                    if (valid) {
                        this.initClass();
                        this.classSet(this.classModify);
                        this.axios({
                            method: 'put',
                            url: '/class',
                            data: this.class
                        }).then(function (response) {
                            if (response.data && response.data.msg == 'success') {
                                this.$Message.info('修改成功');
                            }
                            this.initClassModify();
                            this.getTable(this.pageInfo);
                        }.bind(this)).catch(function (error) {
                            //alert(error);
                        });
                        this.modifyModal = false;
                    } else {
                        this.$Message.error('表单验证失败!');
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 1000);
                    }
                })
            },
            /*modal的cancel点击事件*/
            cancel(val) {
                if (val == 0) {
                    this.$Message.info('新增操作取消');
                }
                if (val == 1) {
                    this.$Message.info('编辑操作取消');
                }

            },
            /*table选择后触发事件*/
            change(e) {
                this.setGroupId(e);
            },
            /*通过选中的行设置groupId的值*/
            setGroupId(e) {
                this.groupId = [];
                this.count = e.length;
                for (var i = 0; i <= e.length - 1; i++) {
                    this.groupId.push(e[i].classNum);
                }
            },
            setSingleGroupId(e){
                this.groupId = [];
                this.count = 1;
                this.groupId.push(e);
            },
            /*删除table中选中的数据*/
            del() {
                if(this.groupId != null && this.groupId != ""){
                    this.$Modal.confirm({
                        title: '信息确认',
                        content: '<p>您确定要删除吗?</p>',
                        onOk: () => {
                            this.axios({
                                method: 'delete',
                                url: '/class',
                                data: this.groupId
                            }).then(function (response) {
                                if (response.data && response.data.msg == 'success') {
                                    this.$Message.info('删除成功');
                                }
                                this.getTable(this.pageInfo);
                                this.groupId = null;
                                this.count = 0;
                            }.bind(this)).catch(function (error) {
                                //alert(error);
                            });
                        },
                        onCancel: () => {
                            this.$Message.info('删除记录取消!');
                        }
                    });
                }else{
                    this.$Message.info('至少选择一条记录!');
                }
            },
            getAllClass() {
                this.axios({
                    method: 'get',
                    url: '/class/getAllClasses'
                }).then(function (response) {
                    var listTemp = response.data;
                    for (var i = 0; i < listTemp.length; i++) {
                        this.classList.push({
                            "value": listTemp[i].classNum,
                            "label": listTemp[i].className
                        });
                    }
                }.bind(this)).catch(function (error) {
                    //alert(error);
                });
            }
        }
    }
</script>
